var studentId;
var viewState;
var classes = [];

// 初始化
function init() {
  getClassApi();
  let searchStr = location.search ? location.search.substr(1) : '';
  let serachArr = searchStr.split('&');
  console.log('第1次以 & 分隔：', serachArr);
  console.log(studentId);
  for (var i = 0; i < serachArr.length; i++) {
    serachArr[i] = serachArr[i].split('=');
  }
  console.log('第2次以 = 分隔：', serachArr);
  studentId = Number(serachArr[0][1]);

  if (studentId && serachArr.length == 2) { // 查看
    viewState = serachArr[1][1];
    return viewInit();
  }
  if (studentId) { // 编辑
    return editInit();
  }

  // 新增
  addInit();
}

init();

function addInit() {
  $('#student-title').text('新增学生');
}

function editInit() {
  $('#student-title').text('编辑学生');
  getStudentInfo();
}

function viewInit() {
  $('#student-title').text('查看学生');
  getStudentInfo();

  $('.student-name').prop('disabled', true);
  $('#studentSubmitBtn').prop('disabled', true);
}

// 请求班级信息
function getClassApi() {
  $.ajax({
    url: '/classes/get',
    method: 'GET'
  })
    .done(function (res) {
      console.log('班级信息：', res.data);
      classes = res.data;
      renderClassSelect(res.data);
    })
    .fail(function (err) {
      console.log('请求班级信息失败', err);
    });
}
function renderClassSelect(data) {
  $('.student-class').text('');
  for (var i = 0; i < data.length; i++) {
    var option = `
      <option value="${data[i].classId}">${data[i].className}</option>
    `;
    $('.student-class').append(option);
  }
}

// 请求学生个人详细信息
function getStudentInfo() {
  $.ajax({
    url: '/students/getbyid',
    method: 'GET',
    data: {
      studentId: studentId
    }
  })
    .done(function (res) {
      console.log('请求学生个人详细信息成功：', res);
      let studentName = $('.student-name').val(res.data.studentName);
      let age = $('.student-age').val(res.data.age);
      // let sex = $(':radio:checked').val(res.data.sex);
      let tel = $('.student-tel').val(res.data.tel);
      let classId = $('.student-class').val(res.data.classId);
      let nation = $('.student-nation').val(res.data.nation);
      let address = $('.student-address').val(res.data.address);

      if (res.data.sex == 1) {
        $('input[name=student-sex]')[0].checked = true
        $('input[name=student-sex]')[1].checked = false
      } else if (res.data.sex == 2) {
        $('input[name=student-sex]')[0].checked = false
        $('input[name=student-sex]')[1].checked = true
      }
    })
    .fail(function (err) {
      console.log('请求学生个人详细信息失败：', err);
      layer.msg(err.msg)
    });
}

// 提交数据
$('#studentSubmitBtn').on('click', function () {
  if (studentId) {
    submitEditStudent();
  } else {
    submitAddStudent();
  }
});

function submitEditStudent() {
  let studentName = $('.student-name').val();
  let age = $('.student-age').val();
  let sex = $(':radio:checked').val();
  let tel = $('.student-tel').val();
  let classId = $('.student-class').val();
  let nation = $('.student-nation').val();
  let address = $('.student-address').val();

  let obj = {
    studentId,
    studentName,
    age,
    sex,
    tel,
    classId,
    nation,
    address
  }
  console.log(obj);

  $.ajax({
    url: '/students/edit',
    method: 'POST',
    data: obj
  })
    .done(function (res) {
      console.log('编辑成功：', res);
      layer.msg(res.msg)
    })
    .fail(function (err) {
      console.log('编辑失败：', err);
      layer.msg(err.msg)
    });
}

function submitAddStudent() {
  let studentName = $('.student-name').val();
  let age = $('.student-age').val();
  let sex = $(':radio:checked').val();
  let tel = $('.student-tel').val();
  let classId = $('.student-class').val();
  let nation = $('.student-nation').val();
  let address = $('.student-address').val();

  let obj = {
    studentName: studentName,
    age: age,
    sex: sex,
    tel: tel,
    classId: classId,
    nation: nation,
    address: address
  }
  console.log(obj);

  $.ajax({
    url: '/students/add',
    method: 'POST',
    data: obj
  })
    .done(function (res) {
      console.log('新增成功：', res);
      layer.msg(res.msg)
    })
    .fail(function (err) {
      console.log('新增失败：', err);
      layer.msg(err.msg)
    });
}